/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.lis.model.newmodel;

import com.cxhd.lis.util.DateUtil;
import com.cxhd.lis.util.ToolDateTime;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 *
 * @author cxhd
 */
@SuppressWarnings("serial")
public class LisSqd extends Model<LisSqd> {
      public static final LisSqd lisSqdDao=new LisSqd();
     
    public void updateXX(Controller con) throws Exception{
        findById(con.getPara("pk"))
                .set("PATIENTNAME",con.getPara("brxm"))
                .set("SEX",con.getPara("sex"))
                .set("AGE",con.getPara("age"))
                .set("SECTION",con.getPara("ks"))
                .set("BED_NO",con.getPara("ch"))
                .set("DIAGNOSTIC",con.getPara("lczd"))
                .update();
    }
    public void insertInfo(Controller con,String req) throws Exception{
        Date thisdate=new Date(System.currentTimeMillis());
        String bir=con.getPara("birth");
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        java.sql.Timestamp thisTime=new java.sql.Timestamp(thisdate.getTime());
        java.sql.Timestamp birth=null;
        int age=0;
        if(bir!=null){
            java.util.Date birt=sdf.parse(bir);
            birth=new java.sql.Timestamp(birt.getTime());
            age=DateUtil.getCurrentAgeByBirthdate(bir);
        }
        String ii=getPatientId();
        System.out.println(ii);
        set("PK",getPk())
                .set("CODE",getCode())
                .set("REQUESTTIME",thisTime)
                .set("REQUESTER",req)
                .set("SQDSTATUS","2")
                .set("STAYHOSPITALMODE",con.getPara("lx"))
                .set("PATIENTID",ii)
                .set("PATIENTNAME",con.getPara("name"))
                .set("SEX",con.getPara("sex"))
                .set("BIRTHDAY",birth)
                .set("PHONENO", con.getPara("phone"))
                .set("AGE",age)
                .set("SECTION",con.getPara("ksmc"))
                .set("BED_NO",con.getPara("ch"))
                .set("EXAMINAIM",con.getPara("xmm"))
                .set("EXAMINAIMCODE",con.getPara("xmid"))
                .set("SAMPLETYPE",con.getPara("yb"))
                .set("CYSJ",thisTime)
                .save();
    }
    public void updateInfo(Controller con)throws Exception{
        String bir=con.getPara("birth");
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        java.sql.Timestamp birth=null;
        int age=0;
        if(bir!=null){
            java.util.Date birt=sdf.parse(bir);
            birth=new java.sql.Timestamp(birt.getTime());
            age=DateUtil.getCurrentAgeByBirthdate(bir);
        }
        findById(con.getPara("pk"))
                .set("STAYHOSPITALMODE",con.getPara("lx"))
                .set("PATIENTNAME",con.getPara("name"))
                .set("SEX",con.getPara("sex"))
                .set("BIRTHDAY",birth)
                .set("AGE",age)
                .set("PHONENO", con.getPara("phone"))
                .set("SECTION",con.getPara("ksmc"))
                .set("BED_NO",con.getPara("ch"))
                .set("EXAMINAIM",con.getPara("xmm"))
                .set("EXAMINAIMCODE",con.getPara("xmid"))
                .set("SAMPLETYPE",con.getPara("yb"))
                .update();
    }
    public void deleteInfo(Controller con)throws Exception{
        deleteById(con.getPara("pk"));
    }
    
    
    public String getPk(){
        Record r=Db.use("mysql").findFirst("select max(PK)+1 PK from lis_sqd");
        String pk=r.get("PK")+"";
        return pk;
    }
    public String getPatientId(){
        List<Record> list=Db.use("mysql").find("select * from lis_sqd limit 0,10");
        String  value = "1110";
        if(list.size()>0){
        value = (Integer.parseInt(Db.use("mysql").findFirst("SELECT MAX(PATIENTID) as id from lis_sqd").get("id") + "") + 1) + "";
        }
        return value;
    }
    public String getCode(){
        List<Record> list=Db.use("mysql").find("select * from lis_sqd limit 0,10");
        String  value = "1000001";
        if(list.size()>0){
        value = (Integer.parseInt(Db.use("mysql").findFirst("SELECT MAX(CODE) as id from lis_sqd").get("id") + "") + 1) + "";
        }
        return value;
    }
}
